<?php
class NewscategoryAction extends BaseAction{
	public function addCategory(){
		$this->assign("position","新闻主类别添加");
		if ( $this->isPost() ) {
			$data['type_name'] = addslashes(trim($_POST['name']));
			if ( M("newstype")->where($data)->find() ) {
				$this->assign("jumpUrl","__URL__/addCategory");
				$this->assign("waitSecond",3);
				$this->error("新闻类别已存在！！！");
			} else {
				$result = M("newstype")->add($data);
				if ( $result ) {
					$this->assign("jumpUrl","__URL__/delCategory");
					$this->assign("waitSecond",3);
					$this->success("新闻主类别添加成功！！！");
				} else {
					$this->assign("jumpUrl","__URL__/addCategory");
					$this->assign("waitSecond",3);
					$this->error("新闻主类别添加失败！！！");
				}
			}
		}else {
			$this->display();
		}
	}
	
	public function delCategory(){
		$this->assign("position","新闻主类别添加删除");
		if ( $this->isPost() ){
			$result = M("newstype")->where("id=".$_POST['id'])->delete();
			//echo M("admin")->getLastSql();die;
			if ( $result ){
				echo "success";
			}else {
				echo "failure";
			}
		} else {
			$allCategory = M("newstype")->order("id desc")->select();
			$this->assign("category",$allCategory);
			$this->display();
		}
	}

	public function subtype(){
		$this->assign("position","新闻副类别添加");
		if ( $this->isPost() ) {
			$data['new_s_name'] = addslashes(trim($_POST['new_s_name']));
			if ( M("news_subtype")->where($data)->find() ) {
				$this->assign("jumpUrl","__URL__/subtype");
				$this->assign("waitSecond",3);
				$this->error("新闻类别已存在！！！");
			} else {
				$result = M("news_subtype")->add($data);
				if ( $result ) {
					$this->assign("jumpUrl","__URL__/delsubtype");
					$this->assign("waitSecond",3);
					$this->success("新闻副类别添加成功！！！");
				} else {
					$this->assign("jumpUrl","__URL__/subtype");
					$this->assign("waitSecond",3);
					$this->error("新闻副类别添加失败！！！");
				}
			}
		}else {
			$this->display();
		}
	}

	public function delsubtype(){
		$this->assign("position","新闻主类别添加删除");
		if ( $this->isPost() ){
			$result = M("news_subtype")->where("new_s_id=".$_POST['id'])->delete();
			//echo M("admin")->getLastSql();die;
			if ( $result ){
				echo "success";
			}else {
				echo "failure";
			}
		} else {
			$allCategory = M("news_subtype")->order("new_s_id desc")->select();
			$this->assign("category",$allCategory);
			$this->display();
		}
	}
}